﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    public class TaxPeriodController : AdminBaseController
    {
        //
        // GET: /Admin/Zsqwh/

        /// <summary>
        /// 征收期维护
        /// </summary>
        /// <returns></returns>
        [AuthorityActionFilter("m_TaxPeriod", Order = 1)]
        [LogPageActionFilter("征收期维护", Order = 2)]
        public ActionResult Index()
        {
            return TaxPeriod("");
        }

        public ActionResult TaxPeriod(string message)
        {
            BLL.TaxPeriod taxPeriodBLL = new BLL.TaxPeriod();
            decimal currentYear = decimal.Parse(DateTime.Now.Year.ToString());
            ViewData["currentYear"] = currentYear;
            List<Model.TaxPeriod> TaxPeriodList = new List<Model.TaxPeriod>();
            if (taxPeriodBLL.GetCountByYear(currentYear) == 0)//如果当年没数据，就自动插入12个月数据
            {
                Model.TaxPeriod TaxPeriodModel = new Model.TaxPeriod();
                for (int i = 0; i < 12; i++)
                {
                    TaxPeriodModel = new Model.TaxPeriod();
                    TaxPeriodModel.FYear = currentYear;
                    TaxPeriodModel.FMonth = i + 1;


                    DateTime tempDate = DateTime.Parse(TaxPeriodModel.FYear.ToString() + "-" + TaxPeriodModel.FMonth.ToString() + "-10");
                    DateTime PeriodDate = tempDate;
                    if (tempDate.DayOfWeek.ToString() == "Saturday")//星期六
                    {
                        PeriodDate = tempDate.AddDays(2);
                    }
                    if (tempDate.DayOfWeek.ToString() == "Sunday")//星期天
                    {
                        PeriodDate = tempDate.AddDays(1);
                    }
                    TaxPeriodModel.FEndDate = PeriodDate;
                    TaxPeriodList.Add(TaxPeriodModel);
                }
                taxPeriodBLL.InsertTaxPeriod(TaxPeriodList);
            }
            else//如果有，则读取12个月的数据
            {
                TaxPeriodList = taxPeriodBLL.GetModelList(" FYear=" + currentYear + " ORDER BY FMonth");
            }

            ViewData["message"] = message;
            return View("Index",TaxPeriodList);
        }



        /// <summary>
        /// 修改征收期
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateTaxPeriod()
        {
            string message = string.Empty;
            decimal currentYear = decimal.Parse(Fetch.Post("hdCurrentYear").ToString().Trim());
            BLL.TaxPeriod TaxPeriodBLL = new BLL.TaxPeriod();
            try
            {
                List<Model.TaxPeriod> TaxPeriodList = new List<Model.TaxPeriod>();
                Model.TaxPeriod TaxPeriodModel = new Model.TaxPeriod();

                TaxPeriodModel = new Model.TaxPeriod();
                TaxPeriodModel.FYear = currentYear;
                TaxPeriodModel.FMonth = 1;
                TaxPeriodModel.FEndDate = DateTime.Parse(Request.Params["dtFEndDate1"]);
                TaxPeriodList.Add(TaxPeriodModel);

                TaxPeriodModel = new Model.TaxPeriod();
                TaxPeriodModel.FYear = currentYear;
                TaxPeriodModel.FMonth = 2;
                TaxPeriodModel.FEndDate = DateTime.Parse(Request.Params["dtFEndDate2"]);
                TaxPeriodList.Add(TaxPeriodModel);

                TaxPeriodModel = new Model.TaxPeriod();
                TaxPeriodModel.FYear = currentYear;
                TaxPeriodModel.FMonth = 3;
                TaxPeriodModel.FEndDate = DateTime.Parse(Request.Params["dtFEndDate3"]);
                TaxPeriodList.Add(TaxPeriodModel);

                TaxPeriodModel = new Model.TaxPeriod();
                TaxPeriodModel.FYear = currentYear;
                TaxPeriodModel.FMonth = 4;
                TaxPeriodModel.FEndDate = DateTime.Parse(Request.Params["dtFEndDate4"]);
                TaxPeriodList.Add(TaxPeriodModel);

                TaxPeriodModel = new Model.TaxPeriod();
                TaxPeriodModel.FYear = currentYear;
                TaxPeriodModel.FMonth = 5;
                TaxPeriodModel.FEndDate = DateTime.Parse(Request.Params["dtFEndDate5"]);
                TaxPeriodList.Add(TaxPeriodModel);

                TaxPeriodModel = new Model.TaxPeriod();
                TaxPeriodModel.FYear = currentYear;
                TaxPeriodModel.FMonth = 6;
                TaxPeriodModel.FEndDate = DateTime.Parse(Request.Params["dtFEndDate6"]);
                TaxPeriodList.Add(TaxPeriodModel);

                TaxPeriodModel = new Model.TaxPeriod();
                TaxPeriodModel.FYear = currentYear;
                TaxPeriodModel.FMonth = 7;
                TaxPeriodModel.FEndDate = DateTime.Parse(Request.Params["dtFEndDate7"]);
                TaxPeriodList.Add(TaxPeriodModel);

                TaxPeriodModel = new Model.TaxPeriod();
                TaxPeriodModel.FYear = currentYear;
                TaxPeriodModel.FMonth = 8;
                TaxPeriodModel.FEndDate = DateTime.Parse(Request.Params["dtFEndDate8"]);
                TaxPeriodList.Add(TaxPeriodModel);

                TaxPeriodModel = new Model.TaxPeriod();
                TaxPeriodModel.FYear = currentYear;
                TaxPeriodModel.FMonth = 9;
                TaxPeriodModel.FEndDate = DateTime.Parse(Request.Params["dtFEndDate9"]);
                TaxPeriodList.Add(TaxPeriodModel);

                TaxPeriodModel = new Model.TaxPeriod();
                TaxPeriodModel.FYear = currentYear;
                TaxPeriodModel.FMonth = 10;
                TaxPeriodModel.FEndDate = DateTime.Parse(Request.Params["dtFEndDate10"]);
                TaxPeriodList.Add(TaxPeriodModel);

                TaxPeriodModel = new Model.TaxPeriod();
                TaxPeriodModel.FYear = currentYear;
                TaxPeriodModel.FMonth = 11;
                TaxPeriodModel.FEndDate = DateTime.Parse(Request.Params["dtFEndDate11"]);
                TaxPeriodList.Add(TaxPeriodModel);

                TaxPeriodModel = new Model.TaxPeriod();
                TaxPeriodModel.FYear = currentYear;
                TaxPeriodModel.FMonth = 12;
                TaxPeriodModel.FEndDate = DateTime.Parse(Request.Params["dtFEndDate12"]);
                TaxPeriodList.Add(TaxPeriodModel);

                TaxPeriodBLL.UpdateTaxPeriod(TaxPeriodList);
                message = "征收期修改成功！";
            }
            catch(Exception ex)
            {
                message = "修改失败，原因为："+ ex.ToString();
            }
            return TaxPeriod(message);
        }











    }
}
